//
//  MLAEateryFoodTypeEditVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/12.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAEateryFoodTypeEditVC.h"

@interface MLAEateryFoodTypeEditVC ()

@property (nonatomic, strong) UITextField *nameTxf;


@end

@implementation MLAEateryFoodTypeEditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _nameTxf = [[UITextField alloc] init];
    _nameTxf.placeholder = @"输入商品类型";
    _nameTxf.font = CX_GetNormalFont(14);
    [self.view addSubview:_nameTxf];
    [_nameTxf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.height.offset(30);
        make.right.offset(-15);
        make.top.equalTo(self.titleView.mas_bottom).offset(20);
    }];
    
    UIButton *commitBtn = [self getCommitButtonWithTitle:@"提交"];
    [commitBtn addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-35);
        make.left.offset(15);
        make.right.offset(-15);
        make.height.offset(40);
    }];
    
    if (_model) {
        [self configTitle:@"编辑商品类型"];
        _nameTxf.text = _model.name;
        
    } else {
        [self configTitle:@"添加商品类型"];
    }

}

#pragma mark - Actions
- (void)commitBtnAction {
    //type_id    否    int    添加时不需要传，编辑时需要传
//    name    否    string    类型名称
    
    if (_nameTxf.text.length == 0) {
        [MLAHudManage showMessage:_nameTxf.placeholder];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:_nameTxf.text forKey:@"name"];
    
    if (_model) {
        [params setObject:_model.modelId forKey:@"type_id"];
    }
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopProductProductTypeEdit params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        if (self.model) {
            [MLAHudManage showMessage:@"编辑成功"];
        } else {
            [MLAHudManage showMessage:@"添加成功"];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^{
        
    }];
    
}


@end
